// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from "vue";
import App from "./App";
import router from "./router";
import ElementUI from "element-ui";
import "element-ui/lib/theme-chalk/index.css";
import * as echarts from "echarts";
Vue.prototype.$echarts = echarts;
import { vueBaberrage } from "vue-baberrage";
Vue.use(vueBaberrage);

//全局自定义指令
Vue.directive("time", {
  bind(el, binding) {
    el.innerHTML = Time.getFormatTime(binding.value);
    //使用定时器，每分钟触发一次，更新时间，并且在 unbind 钩子里清除掉
    el.__timeout__ = setInterval(function() {
      //将格式化时间写入指令所在的元素
      el.innerHTML = Time.getFormatTime(binding.value);
    }, 60000);
  },
  unbind(el, binding) {
    // 在 unbind 钩子里清除定时器
    clearInterval(el.__timeout__);
    delete el.__timeout__;
  }
});

// 自定义指令 实现可拖动
Vue.directive("drag", {
  inserted: function(el, binding) {
    el.onmousedown = function(e) {
      e.stopPropagation();
      let disx = e.pageX - el.offsetLeft;
      let disy = e.pageY - el.offsetTop;
      document.onmousemove = function(e) {
        e.preventDefault();
        el.style.left = e.pageX - disx + "px";
        el.style.top = e.pageY - disy + "px";
      };
      document.onmouseup = function() {
        document.onmousemove = document.onmouseup = null;
        //binding 传参，可忽略
        if (binding && binding.value) {
          binding.value.clientX = parseInt(el.style.left);
          binding.value.clientY = parseInt(el.style.top);
        }
      };
    };
  }
});

// 引入vuex
import store from "./store";

Vue.use(ElementUI); //注册使用ElementUI

Vue.config.productionTip = false;

// 路由守卫
router.beforeEach((to, from, next) => {
  const user = localStorage.getItem("loginUser");
  const manage = localStorage.getItem("manageUesr");
  // const userToken = JSON.parse(user);
  // const manageToken = JSON.parse(manage);

  if (to.path == "/center") {
    if (manage) {
      next();
    } else {
      Vue.prototype.$message({
        message: "请先登录",
        type: "warning"
      });
      console.log("请重新登录");
      next("/");
    }
    // console.log(user, manage);

    // console.log(userToken, manageToken);
    // if ((userToken, manageToken)) {
    // }
  }
  next();
});

/* eslint-disable no-new */
new Vue({
  el: "#app",
  router,
  store,
  components: { App },
  template: "<App/>"
});
